﻿using System;
using System.Collections.Generic;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.ScheduleJob.Engine;
using T.FAS.Runtime.ScheduleJob.Manager;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Service
{
    public class ScheduleJobControlService : IScheduleJobControlService
    {
        private static readonly object lockObj = new object();

        private static ScheduleJobControlService _instance = null;

        public static IScheduleJobControlService GetInstance()
        {
            if (_instance == null)
            {
                lock (lockObj)
                {
                    if (_instance == null)
                        _instance = new ScheduleJobControlService();
                }
            }
            return _instance;
        }
        private ScheduleJobControlService()
        {
        }
        public List<ScheduleJobInstanceStore> GetScheduleJobInstances(UserDefineQueryFilter filter)
        {
            return ScheduleJobInstanceManager.GetInstance().GetScheduleJobInstances(filter);
        }

        public ScheduleJobInstanceStore GetScheduleJobInstanceByInstanceID(string instanceID)
        {
            return ScheduleJobInstanceManager.GetInstance().GetScheduleJobInstanceByInstanceID(instanceID);
        }
        public List<ScheduleJobInstanceStore> GetAllActiveJobs()
        {
            var stores = ScheduleJobInstanceManager.GetInstance().GetAllToBeExecutedScheduleJobInstances();
            return stores;
        }

        public List<ScheduleJobExecuteInfo> GetScheduleJobExecuteInfos(List<string> instanceIDs)
        {
            var list = new List<ScheduleJobExecuteInfo>();
            instanceIDs.ForEach(id=>
            {
                var store = ScheduleJobInstanceManager.GetInstance().GetScheduleJobInstanceByInstanceID(id);
                var instance = ScheduleJobWrapper.WrapScheduleJobInstanceStore(store);
                var triggerKey = ScheduleJobKeyUtil.GetTriggerKey(instance);
                var info = StdSchedulerFacotryRepository.GetInstance().GetScheduleJobExecuteInfo(triggerKey);
                info.InstanceID = id;
                info.InstanceName = store.Name;
                info.Description = store.Description;
                list.Add(info);
            });
            return list;
            
        }


        #region 私有方法

        #endregion
    }
}
